
(function () {
    'use strict';

    angular.module('KingAdmin.pages.pdev.device')
        .controller('DeviceCtrl', DeviceCtrl);

    /** @ngInject */
    function DeviceCtrl($scope, $stateParams, $state,$uibModalInstance, DeviceService,DictService, DevgroupService) {
        var kt = this;
        kt.device = {};
        kt.title = '编辑设备';

        $scope.orgTitle = '设备分组';
        $scope.nodes = DevgroupService.getTreeData().result;

        if($stateParams.id){
            if ($stateParams.isView) {
                kt.isView = true;
                kt.title = '查看设备';
            }

            DeviceService.getInfo({id:$stateParams.id},
                function (data) {
                kt.isGroupShow = true;
                kt.device = data;
                if (kt.device.lat == null) {
                    kt.device.lng = 121.48;
                    kt.device.lat = 31.22;
                    kt.device.zoom = 14;
                }
            })
        }else{
            kt.isGroupShow = true;
            kt.isAdd = true;
            kt.title = '添加设备';
            kt.device = {
                lng: 121.48,
                lat: 31.22,
                address: '测试地址',
                zoom: 15
            };
        }

        kt.devtypes = [];
        DictService.getList('DEVICETYPE',function (data) {
            kt.devtypes = data.result;
            kt.isShowDev = true;
        });

        kt.devstatus = [];
        DictService.getList('DEVICESTATUS',function (data) {
            kt.devstatus = data.result;
            kt.isShowStatus = true;
            // $scope.$digest();
        });

        kt.save = function () {
            DeviceService.save(kt.device,function (data) {
                $uibModalInstance.close(true);
            });
        }

        kt.initBap = function () {
            console.log('初始化地图');
            console.log(kt.device.lng);
            kt.map = new BMap.Map("bMap");
            kt.map.centerAndZoom(new BMap.Point(kt.device.lng, kt.device.lat), kt.device.zoom);  // 初始化地图，设置中心点坐标和地图级别
            kt.map.addControl(new BMap.NavigationControl());
            kt.map.addControl(new BMap.ScaleControl());
            kt.map.addControl(new BMap.OverviewMapControl());
            kt.map.enableScrollWheelZoom(true);
            // 创建地址解析器实例
            kt.myGeo = new BMap.Geocoder();

            // 添加新的覆盖点
            var marker = new BMap.Marker(new BMap.Point(kt.device.lng, kt.device.lat));
            kt.map.addOverlay(marker);

            kt.map.addEventListener("click", function (e) {
                // 删除覆盖点
                var allOverlays = kt.map.getOverlays();
                for (var i = 0; i < allOverlays.length; i++) {
                    kt.map.removeOverlay(allOverlays[i]);
                }
                // 添加新的覆盖点
                var marker = new BMap.Marker(e.point);
                kt.map.addOverlay(marker);

                var pt = e.point;
                kt.device.lng = pt.lng;
                kt.device.lat = pt.lat;
                kt.device.zoom = kt.map.getZoom();
                kt.myGeo.getLocation(pt, function (rs) {
                    var addComp = rs.addressComponents;
                    /**
                     * 将获取到的建筑名赋值给$scope.address
                     */
                    // kt.device.address = addComp.province != addComp.city ? addComp.province + addComp.city : addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                    kt.device.address = addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                    /**
                     * 通知angularjs更新视图
                     */
                    console.log(kt.device);
                    $scope.$digest();
                });
            });
        }


        $uibModalInstance.rendered.then(function() {
            kt.initBap();
        });
    }

})();
